"""
    无人机控制权限管理
"""
from drone.drone_api import DroneApi
from dji_redis.redis_client import RedisClient
from business.business_constant import BusinessConstant
redis_client = RedisClient()
drone_api = DroneApi()

"""
    获取权限，如果没有权限直接申请
"""
def get_control(gateway_sn:str):
    key = BusinessConstant.CONTROL_KEY % gateway_sn
    if None == redis_client.get(key) or 0 == redis_client.get(key):
        print('drone is not in control')
        # 获取控制权
        request = {
            "control_keys": [
                "flight"
            ],
            "user_callsign": "adminPC",
            "user_id": "a1559e7c-8dd8-4780-b952-100cc4797da2"
        }
        redis_client.client.set(key, 1, ex=3600)
        drone_api.cloud_control_auth_request(gateway_sn, request)
        # todo 阻塞等待授权同意
        result = True
        # todo 授权成功存入redis 拒绝直接返回无权限
        print(f"获取控制权结果：{result}" )
        return result

def remove_control(gateway_sn:str):
    key = BusinessConstant.CONTROL_KEY % gateway_sn
    redis_client.set(key, 0)

